package CtrLayer;
import java.util.ArrayList;
import DBLayer.*;
import ModelLayer.*;

public class OrderCtr{
	
	private static OrderCtr instance;
	private Order order;
	private ProductCtr productCtr;
	
	public OrderCtr()
	{
		productCtr = productCtr.getInstance();
	}
	
	public static OrderCtr getInstance()
    {
        if(instance==null)
        {
            instance = new OrderCtr();
        }
        return instance;
    }
	
	
	
	public Order createOrder(Customer customer, Employee employee)
	{
		order = new Order(customer, employee);
		return order;
		
	}
	
	public double getPrice(String productID, String productName)
	{
		Product product = null;
		if(productID != null)
		{
			
			product = productCtr.findProduct(Integer.parseInt(productID));
			System.out.println("product fundet" + product.getName());
			
		}
		
		else
		{
			product = productCtr.findByName(productName);
			System.out.println("product fundet" + product.getName());
		}
		
		return product.getSalesPrice();
	}
	
	public double getTotalPrice()
	{
		return order.getTotalPrice();
	}
	
	public void createSale(String amount, String productID, String productName)
	{
		Product product = null;
		/*product = productCtr.findProduct(Integer.parseInt(productID));
		System.out.println("product fundet" + product.getName());
		PartOrder partOrder = new PartOrder(product, Integer.parseInt(amount));
		System.out.println("new partorder lavet" + partOrder);
	
		order.addPartOrder(partOrder);	
	*/
		if(productID != null)
		{
			
			product = productCtr.findProduct(Integer.parseInt(productID));
			System.out.println("product fundet" + product.getName());
			PartOrder partOrder = new PartOrder(product, Integer.parseInt(amount));
			System.out.println("new partorder lavet" + partOrder);
			order.addPartOrder(partOrder);	
		}
		
		else
		{
			product = productCtr.findByName(productName);
			System.out.println("product fundet" + product.getName());
			PartOrder partOrder = new PartOrder(product, Integer.parseInt(amount));
			System.out.println("new partorder lavet" + partOrder);
			order.addPartOrder(partOrder);	
		}
		
		
		
	}
	
	
	public void endSale() throws Exception
	{
		IFDBOrder dbOrder = new DBOrder();
		IFDBPartOrder dbPartOrder = new DBPartOrder();
		IFDBInvoice dbInvoice = new DBInvoice();
		Invoice invoice = new Invoice(order.getTotalPriceWithDiscount());
		dbInvoice.insertInvoice(invoice);
		invoice.setInvoiceNumber(dbInvoice.findMaxInvoiceNo());
		System.out.println("Invoice number changed to right value");
		order.setInvoice(invoice);
		//order.getInvoice().setAmount(order.getTotalPriceWithDiscount());
		dbOrder.insertOrder(order);
		System.out.println("order is created and inserted into the database");
		order.setOrderId(dbOrder.findMaxOrderId());
		System.out.println("Order id changed to right value");
		dbInvoice.insertInvoice(invoice);
		ArrayList<PartOrder> partOrders = order.getPartOrders();
		for(PartOrder partOrder : partOrders)
		{
			dbPartOrder.insertPartOrder(partOrder, order);
		}
		
		System.out.println("It was a succes: A sale has been registret");
	}
	
	
	
	
	
	
	
}
